package com.kaizeli.website.controller;

import com.kaizeli.website.pojo.entity.Payment;
import com.kaizeli.website.pojo.dto.PaymentDTO;
import com.kaizeli.website.response.Result;
import com.kaizeli.website.service.PaymentService;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.Parameter;
import io.swagger.v3.oas.annotations.tags.Tag;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.math.BigDecimal;
import java.util.List;

/**
 * 回款控制器
 */
@Tag(name = "回款管理", description = "回款相关接口")
@RestController
@RequestMapping("/api/payment")
public class PaymentController {

    @Autowired
    private PaymentService paymentService;

    @Operation(summary = "根据项目ID查询回款列表")
    @GetMapping("/project/{projectId}")
    public Result<List<PaymentDTO>> getPaymentsByProjectId(
            @Parameter(description = "项目ID") @PathVariable Long projectId) {
        return paymentService.getPaymentsByProjectId(projectId);
    }

    @Operation(summary = "根据回款状态查询回款列表")
    @GetMapping("/status/{paymentStatus}")
    public Result<List<PaymentDTO>> getPaymentsByPaymentStatus(
            @Parameter(description = "回款状态") @PathVariable String paymentStatus) {
        return paymentService.getPaymentsByPaymentStatus(paymentStatus);
    }

    @Operation(summary = "创建回款记录")
    @PostMapping
    public Result<Payment> createPayment(@RequestBody Payment payment) {
        return paymentService.createPayment(payment);
    }

    @Operation(summary = "更新回款记录")
    @PutMapping("/{id}")
    public Result<Payment> updatePayment(
            @Parameter(description = "回款记录ID") @PathVariable Long id,
            @RequestBody Payment payment) {
        payment.setId(id);
        return paymentService.updatePayment(payment);
    }

    @Operation(summary = "更新回款状态")
    @PutMapping("/{id}/status")
    public Result<Void> updatePaymentStatus(
            @Parameter(description = "回款记录ID") @PathVariable Long id,
            @Parameter(description = "回款状态") @RequestParam String paymentStatus) {
        return paymentService.updatePaymentStatus(id, paymentStatus);
    }

    @Operation(summary = "计算项目总回款金额")
    @GetMapping("/project/{projectId}/total-amount")
    public Result<BigDecimal> calculateTotalPayment(
            @Parameter(description = "项目ID") @PathVariable Long projectId) {
        return paymentService.calculateTotalPayment(projectId);
    }

    @Operation(summary = "计算项目剩余待回款金额")
    @GetMapping("/project/{projectId}/remaining-amount")
    public Result<BigDecimal> calculateRemainingAmount(
            @Parameter(description = "项目ID") @PathVariable Long projectId) {
        return paymentService.calculateRemainingAmount(projectId);
    }

    @Operation(summary = "批量创建回款记录")
    @PostMapping("/batch")
    public Result<Void> batchCreatePayments(@RequestBody List<Payment> payments) {
        return paymentService.batchCreatePayments(payments);
    }

    @Operation(summary = "删除回款记录")
    @DeleteMapping("/{id}")
    public Result<Void> deletePayment(
            @Parameter(description = "回款记录ID") @PathVariable Long id) {
        return paymentService.deletePayment(id);
    }

    @Operation(summary = "根据ID查询回款记录详情")
    @GetMapping("/{id}")
    public Result<com.kaizeli.website.pojo.dto.PaymentDTO> getPaymentById(
            @Parameter(description = "回款记录ID") @PathVariable Long id) {
        return paymentService.getPaymentById(id);
    }
}
